﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using System.Reflection;
using Cn.Cellula.Client.ControlHelper;
using Cn.Cellula.Model.Content;
using Cn.Edwon.Web;
using Cn.Edwon.Util;
using System.Text.RegularExpressions;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class ItemSearch : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            GvHelper.AppendEventHandler(ref this.gvItem);
            if (!Page.IsPostBack) {
                LoadEOList();
            }
        }
        private String FilterKeywords(String rContent) {
            //String partten = "^[^~`!@#\\$%\\^&*\\(\\)\\-_\\+=\\\\\\]\\[\\}\\{;:'\"/\\?\\.,\\>\\<]+.*$";
            //if(!String.IsNullOrEmpty(rContent) && !Regex.IsMatch(rContent, partten)) {
            //   return rContent;
            //}
            //return "";
            if (String.IsNullOrEmpty(rContent)) {
                return rContent;
            } else if(rContent.Length<2){
                return "";
            }else{
                String p = "`~!@#$%^&*()_+{}|:\"<>?-=[]\\';/.,";
                String content = "";
                for (int i = 0; i < rContent.Length; i++) {
                    if (p.IndexOf(rContent[i]) ==-1) {
                        content = rContent.Substring(i);
                        break;
                    }
                }
                return content;
            }
        }

        public String MarkKeywords(Object rContent) {
            String content = "";
            if (rContent != null) {
                String kw = this.txt_Keywords.Text;
                content = HtmlUtil.CleanHtml(rContent.ToString());
                if (!String.IsNullOrEmpty(kw) && content.IndexOf(kw) != -1) {
                    content = content.Replace(this.txt_Keywords.Text, this.hid_KwFormat.Value.Replace("{0}", this.txt_Keywords.Text));
                } else {
                    content = StrUtil.Cut(content, 100, "...");
                }
                return content;
            } else {
                return "";
            }
        }

        private void LoadEOList() {
            String kw = this.txt_Keywords.Text;
            kw = FilterKeywords(kw);
            this.txt_Keywords.Text = kw;
            if (String.IsNullOrEmpty(kw)) return;
            String hql = " from VItemSearchEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and (eo.VisiableRoleLevel is null or eo.VisiableRoleKey='" + this.Power.RoleKey + "' or eo.VisiableRoleLevel<" + this.Power.RoleLevel + ") ";
            hql += " and (eo.DeptLevel is null or eo.DeptKey='" + this.Power.DeptKey + "' or eo.DeptLevel<" + this.Power.DeptLevel + ") ";
            hql += " and ( ";
            hql += " eo.ItemName like '%" + this.txt_Keywords.Text + "%' ";
            hql += " or eo.ItemTags like '%" + this.txt_Keywords.Text + "%' ";
            hql += " or eo.ItemDesc like '%" + this.txt_Keywords.Text + "%' ";
            hql += " or eo.AtlContents like '%" + this.txt_Keywords.Text + "%' ";
            hql += " or eo.FileNames like '%" + this.txt_Keywords.Text + "%' ";
            hql += " )";
            hql += " order by eo.UpdateDt desc ";  
            IList<VItemSearchEO> list = biz.Find<VItemSearchEO>(hql);

            this.gvItem.DataSource = list;
            this.gvItem.Attributes[GvHelper.ATTR_TOTAL_COUNT] = (list != null) ? list.Count.ToString() : "0";
            this.gvItem.DataBind();
        }
        protected void gvPager_OnSelectedIndexChanged(object sender, EventArgs e) {
            DropDownList ddl = (DropDownList)sender;
            this.gvItem.PageIndex = Convert.ToInt32(ddl.SelectedValue) - 1;
            LoadEOList();
        }
        protected void gvItem_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvItem.PageIndex = e.NewPageIndex;
            LoadEOList();
        }

        protected void btn_Search_Click(object sender, EventArgs e) {
            LoadEOList();
        }
    }
}